package com.kantboot.business.water.service.impl;

import com.kantboot.business.water.domain.entity.BusWaterVip;
import com.kantboot.business.water.repository.BusWaterVipRepository;
import com.kantboot.business.water.service.IBusWaterVipService;
import com.kantboot.system.user.domain.entity.SysUserBalance;
import com.kantboot.system.user.service.ISysUserBalanceService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 提水点服务实现
 */
@Service
public class BusWaterVipServiceImpl
        implements IBusWaterVipService {

    @Resource
    private BusWaterVipRepository repository;

    @Resource
    private ISysUserBalanceService userBalanceService;

    @Resource
    private IUserSelfService userSelfService;

    @Override
    public List<BusWaterVip> getAll() {
        return repository.findAll();
    }

    @Override
    public BusWaterVip getByUserId(Long userId) {
        SysUserBalance balanceSelf = userBalanceService.getBalanceByCodeAndUserId("totalConsumption", userId);
        List<BusWaterVip> byNeedQuotaLessThan = repository.findByNeedQuotaLessThan(balanceSelf.getNumber());
        if (byNeedQuotaLessThan.isEmpty()) {
            return null;
        }
        return byNeedQuotaLessThan.get(0);
    }

    @Override
    public BusWaterVip getSelf() {
        return getByUserId(userSelfService.getSelf().getId());
    }

    @Override
    public BusWaterVip getById(Long id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public PageResult getBodyData(PageParam<BusWaterVip> pageParam) {
        Page<BusWaterVip> bodyData = repository.getBodyData(pageParam.getPageable());
        return PageResult.of(bodyData);
    }
}
